package com.zz.cc.business.app;

import java.io.InputStream;

import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.ViewFlipper;

import com.zz.cc.business.R;
import com.zz.cc.business.R.anim;
import com.zz.cc.business.R.id;
import com.zz.cc.business.R.layout;
import com.zz.common.app.BaseActivity;
import com.zz.common.utils.DeviceUtil;
import com.zz.common.utils.ImageUtil;
import com.zz.common.utils.ZLog;

public class UserGuideActivity extends BaseActivity 
	implements OnClickListener, OnTouchListener, GestureDetector.OnGestureListener {
	
	private static final String TAG = "UserGuideActivity";
	
	private ViewFlipper mViewFlipper;
	private Button mDirectionView;
	
	private int mScreenWidth;
	private int mScreenHeight;
	
	private GestureDetector mDetetor;
	
	private int mCurrentIndex;
	private String []mGuides = new String[] {
			"guide/guide00.jpg",
			"guide/guide01.jpg",
			"guide/guide02.jpg"
	};
	
	@Override
	public final void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mScreenHeight = DeviceUtil.getScreenHeight(this);
		mScreenWidth = DeviceUtil.getScreenWidth(this);
		
		setContentView(R.layout.activity_user_guide);
		initUI();
		initGuides();
	}
	
	private void initUI() {
		mViewFlipper = (ViewFlipper) findViewById(R.id.guide_image_container);
		mDirectionView = (Button) findViewById(R.id.guide_redirect);
		mDirectionView.setOnClickListener(this);
		
		mViewFlipper.setInAnimation(getInAnimation());
		mViewFlipper.setOutAnimation(getOutAnimation());
		mViewFlipper.setOnTouchListener(this);
		
		mDetetor = new GestureDetector(this, this);
	}
	
	private void initGuides() {
		AssetManager am = getAssets();
		for(String path: mGuides) {
			InputStream is = null;
			try {
				is = am.open(path);
				Bitmap b = ImageUtil.decodeStream(is);
				mViewFlipper.addView(makeView(b));
			} catch (Throwable t) {
				t = null;
			} finally {
				if(null != is) {
					try {
						is.close();
					} catch(Exception e) {
						e = null;
					}
				}
			}
		}
	}
	
	private View makeView(Bitmap b) {
		ImageView imageView = new ImageView(this);
		imageView.setLayoutParams(new LayoutParams(mScreenWidth, mScreenHeight));
		imageView.setImageBitmap(b);
		imageView.setScaleType(ScaleType.FIT_XY);
		imageView.setClickable(false);
		imageView.setEnabled(false);
		return imageView;
	}

	@Override	//View.OnClickListener
	public void onClick(View view) {
		int id = view.getId();
		if(id == R.id.guide_redirect) {
			if(mViewFlipper.isFlipping()) {
				return;
			}
			if(mCurrentIndex == mGuides.length - 1) {
				//the last guide image
				onEndOfGuide();
			} else {
				showNextGuide();
			}
		}
	}
	
	@Override	//View.OnTouchListener
	public boolean onTouch(View v, MotionEvent event) {
		mDetetor.onTouchEvent(event);
		return true;
	}
	
	@Override	//GestureDetector.OnGestureListener
	public boolean onDown(MotionEvent e) {
		return false;
	}

	@Override	//GestureDetector.OnGestureListener
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		if(mViewFlipper.isFlipping()){
			return false;
		}
		if(Math.abs((e1.getY() - e2.getY())) < 30){
			return false;
		}
		if((e1.getY() - e2.getY()) > 30){
			if(mCurrentIndex == (mGuides.length - 1)){
				onEndOfGuide();
			} else {
				showNextGuide();
			}
		}else if((e1.getY()-e2.getY())<-30){
			if(mCurrentIndex == 0){
				return false;
			} else {
				showPreGuide();
			}
		}
		return true;
	}

	@Override	//GestureDetector.OnGestureListener
	public void onLongPress(MotionEvent e) {
	}

	@Override	//GestureDetector.OnGestureListener
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		return false;
	}

	@Override	//GestureDetector.OnGestureListener
	public void onShowPress(MotionEvent e) {
		
	}

	@Override	//GestureDetector.OnGestureListener
	public boolean onSingleTapUp(MotionEvent e) {
		return false;
	}
	
	private void showNextGuide() {
		mViewFlipper.setInAnimation(getInAnimation());
		mViewFlipper.setOutAnimation(getOutAnimation());
		mViewFlipper.showNext();
		mCurrentIndex ++;
	}
	
	private void showPreGuide() {
		mViewFlipper.setInAnimation(getPrivousInAnimation());
		mViewFlipper.setOutAnimation(getPrivousOutAnimation());
		mViewFlipper.showPrevious();
		mCurrentIndex --;
	}
	
	private void onEndOfGuide() {
		ZLog.d(TAG, "onEndOfGuide");
		Intent intent = new Intent(this, LoginActivity.class);
		startActivity(intent);
		finish();
		overridePendingTransition(R.anim.activity_slide_right_in, R.anim.activity_slide_left_out);
	}
	
	private Animation getInAnimation() {

		Animation animation = new TranslateAnimation(
				Animation.RELATIVE_TO_SELF, 1, Animation.RELATIVE_TO_SELF, 0,
				Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
		animation.setDuration(250);
		return animation;
	}
	
	private Animation getOutAnimation() {
		Animation animation = new TranslateAnimation(
				Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, -1,
				Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
		animation.setDuration(250);
		return animation;
	}
	
	private Animation getPrivousInAnimation() {
		Animation animation = new TranslateAnimation(
				Animation.RELATIVE_TO_SELF, -1, Animation.RELATIVE_TO_SELF, 0,
				Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
		animation.setDuration(250);
		return animation;
	}
	
	private Animation getPrivousOutAnimation() {
		Animation animation = new TranslateAnimation(
				Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1,
				Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
		animation.setDuration(250);
		return animation;
	}
}
